package user

import (
	"context"
	"strconv"

	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/i18n"
	"mall/service/backend/rpc/backendclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserProfileLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetUserProfileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserProfileLogic {
	return &GetUserProfileLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetUserProfileLogic) GetUserProfile() (resp *types.ProfileResp, err error) {
	var userId string = l.ctx.Value("userId").(string)
	if i, err := strconv.ParseInt(userId, 10, 64); err == nil && i > 0 {
		data, err := l.svcCtx.BackendRpc.GetUserById(l.ctx, &backendclient.UserIdReq{
			Id: uint64(i),
		})
		if err != nil {
			return nil, err
		}

		return &types.ProfileResp{
			BaseDataInfo: types.BaseDataInfo{
				Success: true,
				Msg:     l.svcCtx.Trans.Trans(l.ctx, i18n.Success),
			},
			Data: types.ProfileInfo{
				Username: data.Username,
				Nickname: data.Nickname,
				Avatar:   data.Avatar,
				Mobile:   data.Mobile,
				Email:    data.Email,
			},
		}, nil
	}

	return nil, err
}
